package someTestExcemple.ioStream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class FileCopyTest {
    public static void main(String[] args) throws Exception {
        test1();
    }
    //实现文件复制练习
    public static void test1() throws Exception {
        try (//创建一个文件输入流管道-与文件联通
             InputStream inputStream = new FileInputStream("src/main/java/someTestExcemple/ioStream/940Process.png");
             //创建一个文件输出流管道- 这里这个copy文件先要不存在，如果存在，则覆盖
             OutputStream outputStream = new FileOutputStream("src/main/java/someTestExcemple/ioStream/copy940.png");
             //什么是资源: 就是实现了AutoCloseable接口的类
             //这种try-with-resource语法，自动帮我们关流
             ) {

            byte[] readBuffer = new byte[1024]; //按1kb读取
            int len; //读取或者写入的字节数
            while ((len = inputStream.read(readBuffer)) != -1) {
                outputStream.write(readBuffer, 0, len);
            }
            //后开先关 比较规范
//            outputStream.close();
//            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
